package com.GUI.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算机     优化
public class TestCalc02 {
    public static void main(String[] args) {
        new Claculator().loadFrame();
    }
}
class Claculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        //一个标签‘+ ’
        Label label = new Label("+");
        //监听器
        button.addActionListener(new MyClaculatorListener());

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        //Frame的修饰
        pack();
        setVisible(true);
    }

    //监听器类
    private class MyClaculatorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            //获得加数和被加数
            int i = Integer.parseInt(num1.getText());
            int j = Integer.parseInt(num2.getText());
            //= 进行加法运算，放到第三个框
            //括号是防止i和j被当作字符去运算
            num3.setText("" + (i+j));
            //清除前两个框
            num1.setText(null);
            num2.setText(null);
        }
    }
}

